/*model for search result*/
import config from '../config';
import {requestWithAuth} from '../utils/request';
import { Toast } from 'antd-mobile';

// eslint-disable-next-line
import { getConfirmation } from 'history/DOMUtils';
export default {
    namespace:'classrelation',
    state:{
      unLearn:[],
      hasLearn:[],
      finLearn:[],
      cart:[],
    },
    subscriptions:{
      setup({ dispatch, history}){
        dispatch({type:'getrelation'});
      }
    },
    effects:{
      *getrelation({payload},{put}){
        const {data} = yield requestWithAuth(config.requestUrl + '/api/v1/lectureR');
        yield put({type:'updaterelation', payload:{data}});
      },
      *getCart({payload},{put}){
        const {data} = yield requestWithAuth(config.requestUrl + '/api/v1/cart');
        yield put({type:'updatecart', payload:{data}});
      },
      *addincart({payload:{cid}},{put}) {
        const params = {
          cid,
        }
        const { data }  = yield requestWithAuth(`http://127.0.0.1:7001/api/v1/cart/`,{
          method: 'post',
          body:JSON.stringify(params)
        })
        console.log(data);
        if (data) {
          switch (data.code) {
            case '20100':
              Toast.success('成功添加到购物车');
              break;
            default:
              Toast.fail(data.err);
              break;
          }
        } else {
          Toast.fail('发生未知错误');
        }
      },
    },
    reducers:{
      updatecart(state,{payload:{data}}){
        return {...state, cart:data};
      },
      updaterelation(state, {payload:{data}}){
        let unLearn=[];
        let hasLearn=[];
        let finLearn=[];
        data.map(val => {
          if (val.learn === 1) unLearn.push(val);
          if (val.learn === 2) hasLearn.push(val);
          if (val.learn === 3) finLearn.push(val);
          return val;
        })
        return {...state, unLearn, hasLearn, finLearn};
      }
    },


  }
  